//index.js
//获取应用实例
const app = getApp()
var util = require('../../utils/time.js');
Page({
  data: {
    publicUrl: app.globalData.publicUrl,
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    page:1,
    type:'',
    tid:'',
    height:0,
    //底部导航
    navbar1:[
      {
        name: '广场',
     
        icon:'../image/squire1.png'
      },
    ],
    navbar2: [
     {
        name: '我的',
       
        icon: '../image/our2.png'
      }

    ]
  },
  stopTouchMove: function () {
    return false;
  },
  tabright:function(){
    let userid = wx.getStorageSync('user_id')
    wx.navigateTo({
      url: '../person/person?id=' + userid,
      success:function(){
        console.log('成功')
      },
      fail:function(){
        console.log('失败')
      }
    })
  },
  bindChange: function (e) {

    var that = this;
    that.setData({ currentTab: e.detail.current });
  },
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    
    that.setData({
      tid: e.target.dataset.id
    })
  
  },
  wss: function (e) {
    
    console.log('tid是:'+e.target.dataset.id)
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    /**暂时隐藏 请求数据，通过发布类型的不同请求数据 **/
    wx.request({
      url: that.data.publicUrl + 'index_order',
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data:{
        tid:e.target.dataset.id,
        page:1
      },
      success:function(res){
        that.setData({
          tid: e.target.dataset.id
        })
        console.log(res);
          if(res.data.data != ''){
            that.setData({
              contentlist: res.data.data,
              height: res.data.data.length*184
            })
          }else{
            wx.showToast({
              title: '暂无任务',
              icon: 'none',
              duration: 2000
            })
            that.setData({
              contentlist: res.data.data,
              tid: e.target.dataset.id
            })
          }
      },
      fail:function(res){

      }
    })
  },
  refresh:function(e){
    var that = this
    console.log('tid是:'+that.data.tid);
    that.setData({
      page:that.data.page+1,
    })
    if(that.data.tid == ''){
      that.setData({
        tid: e.currentTarget.dataset.tid
      })
    }else{
      that.setData({
        tid: that.data.tid
      })
    }
    wx.request({
      url: that.data.publicUrl + 'index_order',
      method:'POST',
      header:{'Content-Type': 'application/x-www-form-urlencoded'},
      data:{
        tid: that.data.tid,
        page:that.data.page
      },
      success:function(res){
        console.log('这是');
        console.log(res);
            if(res.data.data == ''){
              that.setData({
                page:0
              })
              that.refresh();
            }
          that.setData({
            contentlist : res.data.data
          })
      },
      fail:function(res){
      }
    })
  },

  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  },
  //事件处理函数
 /*  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  }, */
  onShow:function(){
    var TIME = util.formatTime(new Date());
    console.log(wx.getStorageSync('openid'));
    var that = this
    that.setData({
      page:1,
    })
    var timestamp = Date.parse(new Date());
    var timestamp = timestamp / 1000;
    that.setData({
      timestamp: timestamp
    })
    // 授权
    wx.getSetting({
      success: res => {
        console.log(res)
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          console.log('已授权')
          var openid = wx.getStorageSync('openid')
          if (openid == '' || openid == undefined) {
            wx.reLaunch({
              url: '/pages/bangding/bangding',
            })
          } 
        } else {
          console.log('没授权')
          wx.reLaunch({
            url: '/pages/bangding/bangding',
          })
        }
      }
    })
    //  console.log("当前时间戳为：" + timestamp);
    var chatime = timestamp - '1536298137'
    //  console.log('值为:' + chatime)
    /***暂时隐藏 进入首页 默认加载的任务类型 ****/
     
    // 轮播图
    wx.request({
      url:that.data.publicUrl+'brand',
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data:{
        
      },
      success:function(res){
       // console.log(res);
        that.setData({
          imgUrls:res.data.data
        })
      },
      fail:function(res){
        wx.showToast({
          title: '暂无网络，请确定手机是否联网',
          icon: 'none',
          duration: 3000
        })
      }
    })
    // 类型
    wx.request({
      url: that.data.publicUrl + 'type',
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {

      },
      success: function (res) {
       //  console.log(res.data.data[0].id);
        that.setData({
          type: res.data.data,
         
        })
        if(that.data.type){
          that.numlist();
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '暂无网络，请确定手机是否联网',
          icon: 'none',
          duration: 3000
        })
      },
     
    })
  },
  numlist:function(){
    var that = this
    // 类型数据列表
    wx.request({
      url: that.data.publicUrl + 'index_order',
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        tid: that.data.type[0].id,
        page: that.data.page
      },
      success: function (res) {
        console.log('hahha');
        console.log(res.data.data.length);
        console.log(res);
        that.setData({
          contentlist: res.data.data,
          tid: that.data.type[0].id,
          height: res.data.data.length*184
        })
      },
      fail: function (res) {
        wx.showToast({
          title: '暂无网络，请确定手机是否联网',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  onLoad: function () {
    
    //获取当前时间戳
    
   
    var that = this;

    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },


})
